package com.xw.echo.core.cluster;

import com.xw.echo.core.register.HostAndPort;

import java.util.List;
import java.util.Random;

public class RandomLoadBalance implements LoadBalancer{
    private final Random random = new Random();
    @Override
    public String select(List<HostAndPort> hostAndPorts) {
        if (hostAndPorts == null || hostAndPorts.isEmpty()) {
            return null; // 或者抛出异常，取决于你的错误处理策略
        }
        int nextIndex = random.nextInt(hostAndPorts.size());
        HostAndPort selectedHost = hostAndPorts.get(nextIndex);
        return selectedHost.getHost() + ":" + selectedHost.getPort();
    }
}
